{extend name="public/pure"}
{block name="style"}
<title>管理收货地址</title>
<link rel="stylesheet" href="/home/css/address/index.css">

{/block}
{block name="body"}
{if condition="empty($data)"}
<div class="noAddress">
    <img src="/home/images/commodity/noAddress.png" alt="">
</div>
{else/}
{volist name="data" id="vo"}
<div class="box" data-id="{$vo.id}">
    <div class="header">
        <div class="top">
            <span class="name fl">{$vo.addressee}</span>
            <span class="phone fr">{$vo.tel}</span>
        </div>
        <p class="address">{$vo.address}</p>
    </div>
    <div class="bottom">
        <div class="fl default">
            {if condition="($vo['default_address'] == 1)"}
            <img src="/home/images/commodity/choose_.png"/>
            {else /}
            <img src="/home/images/commodity/choose.png"/>
            {/if}

            <span>默认地址</span>
        </div>
        <div class="fr edit">
            <div class="fl editor">
                <a href="{:Url('Address/edit?id='.$vo['id'])}">
                    <img src="/home/images/commodity/editor.png"/>
                    <span>编辑</span>
                </a>
            </div>
            <div class="fr detail" onclick="detail(this,{$vo.id})">
                <img src="/home/images/commodity/default.png"/>
                <a>删除</a>
            </div>
        </div>
    </div>
</div>
{/volist}
{/if}


<div class="button">
    <a href="/home/address/edit.html">
        <input type="button" class='btn' value="新增收货地址"/>
    </a>
</div>

{/block}
{block name="script"}

<script>

    window.addEventListener('pageshow', function(event) {
        if (event.persisted) location.reload();    //如果检测到页面是从“往返缓存”中读取的，刷新页面
    });

    // 默认选择
    $('.default').on('click',function(){
        $(".default").find("img").attr('src','/home/images/commodity/choose.png');
        if( $(this).find('img').attr('src')=='/home/images/commodity/choose.png'){
            $(this).find('img').attr('src','/home/images/commodity/choose_.png')
        }else{
            $(this).find('img').attr('src','/home/images/commodity/choose.png')
        }
        var id = $(this).parents(".box").attr("data-id");
        $.ajax({
            type: "post",
            url:"{:Url('Address/mr')}",
            data: {
                id: id
            },
            success: function(data){

            }
        })
    });


    // 删除功能
    function detail(this_,id){
        $.ajax({
            type: "post",
            url:"{:Url('address/del')}",
            data: {
                id:id
            },
            success: function(data){
                //成功后刷新页面
                if(data.code == 1){
                    swal({
                        type: "success",
                        title: "",
                        text:"删除成功",
                        timer: 1500,
                        showConfirmButton: false
                    });
                    setTimeout(function () {
                        $(this_).parents(".box").remove();
                    },1500)
                }else{
                    setTimeout(function(){
                        swal("删除失败","",'error')
                    },200);
                }
            }
        })

    }


</script>
{/block}